<template>
  <div class="newest">
    <div class="newest-resources">
      <div class="newest-resources-container">
        <div
          class="left-column"
          :style="{
            backgroundImage: `url(${
              diyige.images.length > 0
                ? diyige.images[0]
                : require('../../assets/img/NewestResources.png')
            })`,
          }"
        >
          <p class="img-caption" :title="diyige.Content">
            {{ diyige.Content }}
          </p>
        </div>
        <div class="right-column">
          <div class="title-bar">
            <span class="new-tag">
              <i class="iconfont icon-zuixin"></i>
            </span>
            <h3>最新资源</h3>
            <a href="#" class="more-link" @click.prevent="handleLoadMore">
              换一换
              <!-- <i class="iconfont icon-z044-copy"></i> -->
            </a>
          </div>
          <ul class="resource-list">
            <li
              v-for="(item, index) in resources"
              :key="index"
              class="resource-item"
              @click="handleItemClick(item, index)"
                v-loading="loading"
            >
              <span class="item-title" :title="item.Content">{{
                item.Content
              }}</span>
              <span class="item-date">{{ item.createTime }}</span>
            </li>
            <div class="loading-tip" v-if="loading">加载中...</div>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import service from "@/service/service";

export default {
  name: "NewestResources",
  data() {
    return {
      resources: [],
      diyige: {
        images: [],
      },
      currentPage: 1,
      pageSize: 6,

      hasMore: true,
      loading: false,
    };
  },
  mounted() {
    this.getNewestResources();
  },
  methods: {
    handleItemClick(item) {
      if (item.type?.typeName === "图书") {
        this.$router.push({
          path: "/BookDetailsPage",
          query: { id: item.id },
        });
      } else {
        this.$router.push({
          path: "/DetailsPage",
          query: { id: item.id },
        });
      }
    },
    async getNewestResources() {
      try {
        this.loading = true;
        const res = await service({
          url: "/frontend/index/latestResourceList",
          method: "post",
          data: {
            pageNo: this.currentPage,
            pageSize: this.pageSize,
          },
        });
        if (res.code === 200) {
          if (res.data.list.length === 0) {
            this.$message.error("暂无数据");
          } else {
            const newResources = res.data.list.map((item) => {
              return {
                ...item,
                Content: `[${item.resourceTag?.tagName || ""}${
                  item.resourceTag?.tagName && item.type?.typeName ? " | " : ""
                }${item.type?.typeName || ""}] ${item.title}`,
              };
            });

            this.resources = newResources;
            this.diyige = newResources[0] || {};
          }
        } else {
          this.$message.error(res.msg || "数据获取失败");
        }
      } catch (error) {
        this.$message.error(error || "数据获取失败");
      } finally {
        this.loading = false;
      }
    },

    handleLoadMore() {
      if (!this.hasMore || this.loading) return;
      this.currentPage++;
      this.getNewestResources();
    },
  },
};
</script>

<style lang="scss" scoped>
.newest {
  display: flex;
  justify-content: center;
}

.newest-resources {
  display: flex;
  border-radius: 6px;
  width: 1400px;
  height: 427px;
  background: rgba(255, 255, 255, 0.4);
  border-radius: 20px;
  padding: 10px;

  .newest-resources-container {
    background: #fff;
    border-radius: 20px;
    display: flex;
    padding: 20px;
    width: 100%;
  }

  .left-column {
    width: 619px;
    margin-right: 20px;
    background-size: 100% 100%;
    // background-image: url("../../assets/img/NewestResources.png");
    background-position: center;
    border-radius: 4px;
    position: relative;
    overflow: hidden;

    .img-caption {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 608px;
      line-height: 50px;
      min-height: 50px;
      background: rgba(0, 0, 0, 0.6);
      border-radius: 0px 0px 10px 10px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 22px;
      color: #ffffff;
      padding: 0 10px; // 添加内边距，避免文字贴边
      white-space: nowrap; // 禁止换行
      overflow: hidden; // 隐藏溢出内容
      text-overflow: ellipsis; // 显示省略号
    }
  }

  .right-column {
    flex: 1;

    .title-bar {
      display: flex;
      align-items: center;
      padding-bottom: 12px;
      border-bottom: 1px solid #e4e4e4;

      .new-tag {
        i {
          font-size: 40px !important;
          color: #ff4242;
        }
        margin-right: 8px;
      }

      h3 {
        font-family: PingFangSC, PingFang SC;
        font-weight: 500;
        font-size: 28px;
        color: #333333;
        margin: 0;
        flex: 1;
      }

      .more-link {
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 18px;
        color: #327dff;
        text-decoration: none;
        transition: color 0.2s;

        &:hover {
          color: #0d47a1;
        }
      }
    }

    .resource-list {
      list-style: none;
      padding: 0;
      margin: 0;

      .resource-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 6px 0;

        &:last-child {
          border-bottom: none;
        }

        .item-type {
          color: #666;
          margin-right: 6px;
          font-size: 13px;
        }

        .item-title {
          width: 497px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 18px;
          color: #333333;
          text-decoration: none;
          transition: color 0.2s;
          padding: 11px 0;
          cursor: pointer;
          &:hover {
            color: #327dff;
          }
        }

        .item-date {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 18px;
          color: #666666;
        }
      }

      .loading-tip,
      .no-more-tip {
        text-align: center;
        padding: 10px 0;
        color: #666;
        font-size: 14px;
      }

      .no-more-tip {
        color: #999;
      }
    }
  }
}
</style>
